package edu.ssdut.revise.core.serialization;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.serialization.JavaParserJsonSerializer;

import javax.json.Json;
import javax.json.stream.JsonGenerator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * parse a source file into json with all the details
 */
public class Detailed2Json {
    public static void parse2Json(String filePath, String outFilePath) throws FileNotFoundException {
        CompilationUnit cu = StaticJavaParser.parse(new File(filePath));
        JavaParserJsonSerializer s = new JavaParserJsonSerializer();
        File outFile = new File(outFilePath);
        OutputStream os = new FileOutputStream(outFile);
        JsonGenerator g = Json.createGenerator(os);
        s.serialize(cu, g);
    }

    public static void parse2Json(CompilationUnit cu, String outFilePath) throws FileNotFoundException {
        JavaParserJsonSerializer s = new JavaParserJsonSerializer();
        File outFile = new File(outFilePath);
        OutputStream os = new FileOutputStream(outFile);
        JsonGenerator g = Json.createGenerator(os);
        s.serialize(cu, g);
    }
}
